package com.chainyoung.trademarket.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.MyPagerSnapHelper;
import android.support.v7.widget.PagerSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;


import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.view.banner.BannerEntry;
import com.chainyoung.common.view.banner.view.BannerView;
import com.chainyoung.common.appcommon.entity.MarketDetail;
import com.chainyoung.component_tmperson.ui.activity.dapp.BrowserActivity;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;
import com.chainyoung.trademarket.R;
import com.chainyoung.trademarket.entity.Ad;
import com.chainyoung.trademarket.entity.FindItem;
import com.chainyoung.trademarket.entity.IndexApp;
import com.chainyoung.trademarket.ui.AdDetailActivity;
import com.chainyoung.trademarket.widgets.PageIndicatorView;
import com.chainyoung.trademarket.widgets.PagingScrollHelper;

import java.util.ArrayList;
import java.util.List;

import static com.chainyoung.common.constant.CommonStringConstants.URL;

public class IndexAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<MarketDetail> marketDetailList;
    private List<IndexApp> indexAppList;
    private List<Ad> banners;
    private List<Ad> ads;

    private final static int TYPE_HEAD = 0;
    private final static int TYPE_LIST = 2;
    private final static int TYPE_MARKET = 1;
    private LayoutInflater inflate;


    public IndexAdapter(Context context, List<MarketDetail> marketList, List<IndexApp> indexAppList, List<Ad> banners, List<Ad> ads) {
        this.mContext = context;
        this.marketDetailList = marketList;
        this.indexAppList = indexAppList;
        this.banners = banners;
        this.ads = ads;
        inflate = LayoutInflater.from(mContext);
    }

    public void setMarketDetailList(List<MarketDetail> marketDetailList) {
        this.marketDetailList = marketDetailList;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEAD;
        } else if (position == 1) {
            return TYPE_MARKET;
        } else {
            return TYPE_LIST;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_HEAD:
                View view = inflate.inflate(R.layout.layout_index_type_head, parent, false);
                return new HeadHolder(view);
            case TYPE_MARKET:
                view = inflate.inflate(R.layout.layout_type_index_market, parent, false);
                return new MarketHolder(view);
            case TYPE_LIST:
                view = inflate.inflate(R.layout.layout_index_type_list, parent, false);
                return new ListHolder(view);

        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEAD:
                HeadHolder headHolder = (HeadHolder) holder;
                headHolder.cardView.setVisibility(banners.isEmpty() ? View.GONE : View.VISIBLE);
                headHolder.viewFlipper.setVisibility(ads.isEmpty() ? View.GONE : View.VISIBLE);
                headHolder.banner.setEntries(getBannerEntries(banners), true);
                headHolder.viewFlipper.removeAllViews();
                headHolder.viewFlipper.stopFlipping();
                for (Ad notice : ads) {
                    TextView textView = (TextView) inflate.inflate(R.layout.layout_notice, null);
                    textView.setTag(notice);
                    textView.setMaxLines(1);
                    textView.setSingleLine(true);
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Ad notice = (Ad) v.getTag();
                            Intent intent = new Intent(mContext, AdDetailActivity.class);
                            intent.putExtra("ad", notice);
                            mContext.startActivity(intent);
                        }
                    });
                    textView.setText(notice.getTitle());
                    headHolder.viewFlipper.addView(textView);
                }
                headHolder.viewFlipper.startFlipping();


                break;

            case TYPE_MARKET:
                MarketHolder marketHolder = (MarketHolder) holder;
                marketHolder.recyclerView.setVisibility(marketDetailList.isEmpty() ? View.GONE : View.VISIBLE);
                marketHolder.pagingScrollHelper.setAdapter(marketHolder.adapter);
                break;
            case TYPE_LIST:
                ListHolder listHolder = (ListHolder) holder;
                listHolder.rvAppList.setAdapter(new IndexAppListAdapter(indexAppList.get(position - 2).getList()));
                listHolder.tvTitle.setText(indexAppList.get(position - 2).getTitle());
                break;
        }
    }


    @Override
    public int getItemCount() {
        return indexAppList.size() + 2;
    }


    class HeadHolder extends RecyclerView.ViewHolder {

        BannerView banner;
        ViewFlipper viewFlipper;
        CardView cardView;


        public HeadHolder(View itemView) {
            super(itemView);

            banner = itemView.findViewById(R.id.vp_view_pager);
            viewFlipper = itemView.findViewById(R.id.viewFlipper);

            cardView = itemView.findViewById(R.id.card_view);

            banner.setOnPageClickListener(new BannerView.OnPageClickListener() {
                @Override
                public void onPageClick(BannerEntry entry, int index) {
                    Ad item = banners.get(index);
                    RouterUtils.getInstance()
                            .build(RouterConstants.PATH_OTHERAPP)
                            .withString("title",item.getUrl())
                            .withString(URL, item.getUrl())
                            .navigation(mContext);
                }
            });


        }

    }

    class MarketHolder extends RecyclerView.ViewHolder {

        RecyclerView recyclerView;
        PageIndicatorView indicatorView;

        private IndexMarketAdapter adapter;
        PagingScrollHelper pagingScrollHelper = new PagingScrollHelper();
       // MyPagerSnapHelper pagerSnapHelper=new MyPagerSnapHelper();

        public MarketHolder(View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.rv_market);
            indicatorView = itemView.findViewById(R.id.indicator_view);
            //cardView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerView.setLayoutManager(layoutManager);

            adapter = new IndexMarketAdapter(mContext, marketDetailList);
            recyclerView.setAdapter(adapter);
           // pagerSnapHelper.attachToRecyclerView(recyclerView);
            pagingScrollHelper.setUpRecyclerView(recyclerView);
            pagingScrollHelper.setIndicator(indicatorView);
            pagingScrollHelper.setOnPageChangeListener(new PagingScrollHelper.onPageChangeLister() {
                @Override
                public void onPageChange(int index) {

                }
            });

        }
    }

    class ListHolder extends RecyclerView.ViewHolder {


        TextView tvTitle;
        RecyclerView rvAppList;


        public ListHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            rvAppList = itemView.findViewById(R.id.rv_app_list);
            GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
            rvAppList.setLayoutManager(layoutManager);
            DividerItemDecoration itemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.HORIZONTAL);
            itemDecoration.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_index_app_divider));
            DividerItemDecoration itemDecoration1 = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
            itemDecoration1.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_index_app_divider_vertical));
            rvAppList.addItemDecoration(itemDecoration);
            rvAppList.addItemDecoration(itemDecoration1);

        }


    }

    @NonNull
    private List<ImageBannerEntry> getBannerEntries(List<Ad> data) {
        List<ImageBannerEntry> items = new ArrayList<>();
        for (Ad findItem : data) {
            items.add(new ImageBannerEntry(mContext, findItem.getTitle(), findItem.getTitle(), findItem.getImg(), findItem.getUrl()));
        }
        return items;
    }

    private boolean startApp(String pkgName) {
        PackageManager packageManager = mContext.getPackageManager();
        Intent intent = new Intent();
        intent = packageManager.getLaunchIntentForPackage(pkgName);
        if (intent == null) {
            return false;
        } else {
            mContext.startActivity(intent);
            return true;
        }
    }

    private void launch(String url, String name) {
        Intent intent = new Intent(mContext, BrowserActivity.class);
        intent.putExtra(URL, url);
        intent.putExtra("title", url);
        mContext.startActivity(intent);
    }


}
